/*
 * Copyright (c) 2011-2012 Alexander Dubu
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * o  Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * o  Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * o  Neither the name Axil nor the names of its contributors may be used to
 *    endorse or promote products derived from this software without specific
 *    prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package axil.vm.stack;

import axil.api.AxilObject;
import axil.api.extend.Source;
import axil.stdlib.core.type.Nil;

import static axil.framework.Functions.*;


/**
 * A stack frame with three arguments.
 */
final class SF2 extends StackFrame {
	private final int ai;
	private final int bi;
	private AxilObject av;
	private AxilObject bv;


	SF2(StackFrame parent, Source source, int a, int b) {
		super(parent, source);
		ai = a;
		bi = b;
	}


	/**
	 * Get the first parameter to this function. The value returned is never
	 * null, but may be the special Axil nil object.
	 */
	public AxilObject a() {
		return av;
	}


	/**
	 * Get the second parameter to this function. The value returned is never
	 * null, but may be the special Axil nil object.
	 */
	public AxilObject b() {
		return bv;
	}


	/**
	 * Get the third parameter to this function. The value returned is never
	 * null, but may be the special Axil nil object.
	 */
	public AxilObject c() {
		return Nil.object;
	}


	public void a(AxilObject v) {
		av = v;
	}
	public void b(AxilObject v) {
		bv = v;
	}
	public void c(AxilObject v) {
		throw unreachable();
	}


	/**
	 * Get the actual number of parameters passed. The value returned is always
	 * greater than or equal to zero. This is used when dealing with an arbitrary
	 * number of arguments to a function.
	 */
	public int size() {
		return 2;
	}


	/**
	 * Get a parameter from the set passed to this function for execution.
	 * The first parameter is in slot zero. The value returned is never
	 * null, but may be the special Axil nil object. Use the size() method when
	 * dealing with an arbitrary number of arguments.
	 */
	public AxilObject at(int index) {
		switch(index) {
		case 0:
			return av;
		case 1:
			return bv;
		default:
			return Nil.object;
		}
	}


	/**
	 * Set the value of the specified stack index variable to the given value.
	 * The value given may be nil but never null. This only sets the value into
	 * the existing frame, never upstream into parent frames.
	 */
	public void at(int index, AxilObject value) {
		if (index == 0) {
			av = value;
			return;
		}
		if (index == 1) {
			bv = value;
			return;
		}
		throw abort("Cannot set that variable in this frame.",
		            nv("index", index));
	}


	/**
	 * Get the value with the given unique ID, or return a null if no such
	 * object can be found in this frame. This does NOT look into the parent
	 * frame.
	 */
	protected AxilObject lookup(int id) {
		if (ai == id) {
			return av;
		}
		if (bi == id) {
			return bv;
		}
		return null;
	}
}
